package cfca.seal.front.service.servlet;

import cfca.sadk.util.Base64;
import cfca.seal.front.service.FunctionType;
import cfca.seal.front.service.ws.IOfficeWordSealService;
import cfca.seal.util.StringUtil;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class OfficeWordSealServlet extends HttpServlet
{
  private static final long serialVersionUID = -8104762669967495626L;
  private static final Logger logger = LoggerFactory.getLogger("service_sys");

  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    request.setCharacterEncoding("UTF-8");
    String type = request.getParameter("type");
    OutputStream os = response.getOutputStream();
    try {
      ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
      IOfficeWordSealService officeWordSealServiceImpl = (IOfficeWordSealService)applicationContext.getBean("officeWordSealServiceImpl");

      if (type.equals(FunctionType.sealAutoOfficeWord.toString()))
      {
        String wordFileString = request.getParameter("wordFile");
        byte[] wordFile = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(wordFileString)) {
          wordFile = Base64.decode(wordFileString.getBytes("UTF-8"));
        }
        String officeWordSealStrategyXML = request.getParameter("officeWordSealStrategyXML");
        if (StringUtil.isNotEmpty(officeWordSealStrategyXML)) {
          officeWordSealStrategyXML = new String(Base64.decode(officeWordSealStrategyXML.getBytes("UTF-8")), "UTF-8");
        }

        byte[] sealAutoOfficeWord = officeWordSealServiceImpl.sealAutoOfficeWord(wordFile, officeWordSealStrategyXML);
        byte[] result = Base64.encode(sealAutoOfficeWord);
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.sealOfficeWordFunction.toString()))
      {
        String wordFileString = request.getParameter("wordFile");
        byte[] wordFile = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(wordFileString)) {
          wordFile = Base64.decode(wordFileString.getBytes("UTF-8"));
        }
        String pfxString = request.getParameter("pfx");
        byte[] pfx = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(pfxString)) {
          pfx = Base64.decode(pfxString.getBytes("UTF-8"));
        }
        String pfxPassword = request.getParameter("pfxPassword");
        if (StringUtil.isNotEmpty(pfxPassword)) {
          pfxPassword = new String(Base64.decode(pfxPassword.getBytes("UTF-8")), "UTF-8");
        }
        String officeWordSealStrategyXML = request.getParameter("officeWordSealStrategyXML");
        if (StringUtil.isNotEmpty(officeWordSealStrategyXML)) {
          officeWordSealStrategyXML = new String(Base64.decode(officeWordSealStrategyXML.getBytes("UTF-8")), "UTF-8");
        }

        byte[] sealOfficeWordFunction = officeWordSealServiceImpl.sealOfficeWordFunction(wordFile, pfx, pfxPassword, officeWordSealStrategyXML);
        byte[] result = Base64.encode(sealOfficeWordFunction);
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.verifyOfficeWordSeal.toString()))
      {
        String sealedWordString = request.getParameter("sealedWord");
        byte[] sealedWord = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(sealedWordString)) {
          sealedWord = Base64.decode(sealedWordString.getBytes("UTF-8"));
        }
        String officeWordVerifyStrategyXML = request.getParameter("officeWordVerifyStrategyXML");
        if (StringUtil.isNotEmpty(officeWordVerifyStrategyXML)) {
          officeWordVerifyStrategyXML = new String(Base64.decode(officeWordVerifyStrategyXML.getBytes("UTF-8")), "UTF-8");
        }

        String verifyOfficeWordSeal = officeWordSealServiceImpl.verifyOfficeWordSeal(sealedWord, officeWordVerifyStrategyXML);
        byte[] result = Base64.encode(verifyOfficeWordSeal.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      }

      if (null != os) {
        os.flush();
        os.close();
      }
    }
    catch (Exception e)
    {
      logger.error("C0001", e);
      StringBuilder result = new StringBuilder();
      result.append("<Error><errorCode>");
      result.append("C0001");
      result.append("</errorCode><ErrorMessage>");
      result.append("错误");
      result.append("</ErrorMessage>");
      byte[] returnValue = Base64.encode(result.toString().getBytes("UTF-8"));
      os.write(returnValue, 0, returnValue.length);

      if (null != os) {
        os.flush();
        os.close();
      }
    }
    finally
    {
      if (null != os) {
        os.flush();
        os.close();
      }
    }
  }
}